package cn.anc.aonicardv.module.ui.my;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.SpUtils;
import cn.anc.aonicardv.widget.CustomDialog;

/**
 * function :隐私协议界面
 * </p>
 * Created by lzj on 2018/9/12.
 */
public class PrivacyActivity  extends BaseActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_privacy);
        TextView mTvTitle = (TextView) findViewById(R.id.tv_privacy_title);
        TextPaint paint = mTvTitle.getPaint();
        paint.setFakeBoldText(true);
        findViewById(R.id.tv_privacy_no_agree).setOnClickListener(this);
        findViewById(R.id.tv_privacy_agree).setOnClickListener(this);
        final TextView mTvContent = (TextView) findViewById(R.id.tv_privacy_content);
        final SpannableStringBuilder style = new SpannableStringBuilder();
        //设置文字
        style.append(getResources().getString(R.string.privacy_content));
        int start = Integer.parseInt(getResources().getString(R.string.privacy_start));
        int end = Integer.parseInt(getResources().getString(R.string.privacy_end));
        //设置部分文字点击事件
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                if (!isFastClick()){
                    PrivacyActivity.this.startActivity(new Intent( PrivacyActivity.this,PrivacyDetailActivity.class));
                }
            }
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setUnderlineText(false);
            }
        };
        style.setSpan(clickableSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置部分文字颜色
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(getResources().getColor(R.color.privacy_content_special_color));
        style.setSpan(foregroundColorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //配置给TextView
        mTvContent.setMovementMethod(LinkMovementMethod.getInstance());
        mTvContent.setHighlightColor(getResources().getColor(R.color.transparent));//去掉点击后的背景颜色为透明
        mTvContent.setText(style);
    }
    @Override
    public void initVariable() {


    }

    @Override
    public void setListener() {

    }

    @Override
    public void initData() {

    }
    private  CustomDialog PrivaceDialog;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_privacy_no_agree:
                if (!isFastClick()){
                    if (PrivaceDialog ==null){
                        PrivaceDialog = new CustomDialog.AlertBuilder(this)
                                .setCanceled(true)
                                .setPositiveBtn("", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }

                                })
                                .setDialogInOutAnimResId(-2)
                                .setDialogWindowBgDimAcount(0.6f)
                                .setCanceledOnTouchOutside(false)
                                .createPrivaceDialog();
                    }
                    PrivaceDialog.show();
                }

                break;

            case R.id.tv_privacy_agree:
                if(!isFastClick()){
                    SpUtils.putBoolean(Constant.SpKeyParam.IS_AGREE_PRIVACY, true);
                    startActivity(new Intent(this,MainActivity.class));
                    finish();

                }
                break;

        }
    }
    /**
     * 防止多次點擊事件
     */
    private static final int MIN_DELAY_TIME= 800;  // 两次点击间隔不能少于800ms
    private static long lastClickTime;

    public static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - lastClickTime) >= MIN_DELAY_TIME) {
            flag = false;
        }
        lastClickTime = currentClickTime;
        return flag;
    }
}
